﻿namespace EFS.RC.CommunicatorContext.Common
{
	public static class Functions
	{
		/// LyncConversationKeepAliveMillisecondsInterval
		public const System.Int32 LyncConversationKeepAliveMillisecondsInterval = 1000;

		public const System.Int32 MillisecondsBetweenRepetitiveAsyncCalls = 30;

		/// Enum OperatorType
		public enum OperatorType
		{
			NotSet,
			Agent,
			Supervisor
		}

		#region Properties
		/// Property HostFullUrl: Get
		public static System.String HostFullUrl
		{
			get
			{
				// crete url
				System.Text.StringBuilder sbUrl = new System.Text.StringBuilder();
				sbUrl.AppendFormat("{0}://{1}:{2}",
					System.Windows.Application.Current.Host.Source.Scheme,
					System.Windows.Application.Current.Host.Source.Host,
					System.Windows.Application.Current.Host.Source.Port);

				// ok
				return sbUrl.ToString();
			}
		}

		/// Property SurveyFullUrl: Get
		public static System.String SurveyFullUrl
		{
			get
			{
				// crete url
				System.Text.StringBuilder sbUrl = new System.Text.StringBuilder();
				sbUrl.AppendFormat("{0}://{1}{2}",
					EFS.RC.CommunicatorContext.Common.Config.Main.SurveyUrlScheme,
					EFS.RC.CommunicatorContext.Common.Config.Main.SurveyUrlHost,
					EFS.RC.CommunicatorContext.Common.Config.Main.SurveyUrlPort.HasValue ? System.String.Format(":{0}", EFS.RC.CommunicatorContext.Common.Config.Main.SurveyUrlPort.Value) : "");

				// ok
				return sbUrl.ToString();
			}
		}
		#endregion

		#region Utilities
		/// StartSurveyExternal
		public static void StartSurveyExternal(
			System.Guid conversationGuid,
			System.Int32 surveyId)
		{
			// crete url
			System.Text.StringBuilder sbUrl = new System.Text.StringBuilder();
			sbUrl.AppendFormat("{0}/index.aspx?cmd=score_ext&cnv_guid={1}&srv_id={2}",
				SurveyFullUrl,
				conversationGuid,
				surveyId);

			// open
			System.Windows.Browser.HtmlPage.Window.Navigate(
				new System.Uri(sbUrl.ToString()),
				"__blank",
				"fullscreen=no,status=yes,toolbar=no,location=yes,menubar=no,directories=no,resizable=yes,scrollbars=yes");
		}

		/// StartSurveyListExternal
		public static void StartSurveyListExternal(
			System.Guid conversationGuid,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewTypeSessions)
		{
			// crete url
			System.Text.StringBuilder sbUrl = new System.Text.StringBuilder();
			sbUrl.AppendFormat("{0}/index.aspx?cmd=list_ext&cnv_guid={1}",
				SurveyFullUrl,
				conversationGuid);
			if (reviewTypeSessions.HasValue)
			{
				sbUrl.AppendFormat("&review_type_sessions={0}", reviewTypeSessions.Value.ToString());
			}

			// open
			System.Windows.Browser.HtmlPage.Window.Navigate(
				new System.Uri(sbUrl.ToString()),
				"__blank",
				"fullscreen=no,status=yes,toolbar=no,location=yes,menubar=no,directories=no,resizable=yes,scrollbars=yes");
		}

		/// Shows an error message
		public static void ShowErrorMessage(System.String error)
		{
			ShowErrorMessage(error, null);
		}

		/// Shows an error message
		public static void ShowErrorMessage(
			System.String error,
			System.Exception ex)
		{
			// create builder
			System.Text.StringBuilder sbErr = new System.Text.StringBuilder();
			sbErr.Append(error);

			// test ex
			System.String exData = null;
			if (null != ex)
			{
				// create builder
				System.Text.StringBuilder sbEx = new System.Text.StringBuilder();

				// message
				if (EFS.Common.Verify.String(ex.Message))
				{
					sbEx.AppendFormat("Message: {0}", ex.Message);
				}
				// stack trace
				if (EFS.Common.Verify.String(ex.StackTrace))
				{
					if (sbEx.Length > 0)
					{
						sbEx.AppendLine();
						sbEx.AppendLine();
					}
					sbEx.AppendFormat("StackTrace:{0}{1}", System.Environment.NewLine, ex.StackTrace);
				}

				// set
				exData = sbEx.ToString();
			}

			// test ex data
			if (EFS.Common.Verify.String(exData))
			{
				sbErr.AppendFormat("{0}{0}EXCEPTION{0}{1}",
					System.Environment.NewLine,
					exData);
			}

			// show
			System.Windows.MessageBox.Show(sbErr.ToString(), "Error", System.Windows.MessageBoxButton.OK);
		}

		/// ShowSuccessMessage
		public static void ShowSuccessMessage(System.String message)
		{
			System.Windows.MessageBox.Show(message, "Operation completed", System.Windows.MessageBoxButton.OK);
		}

		/// ShowBeepMessage
		/// http://silversynth.codeplex.com/
		/// http://www.soundjay.com/beep-sounds-3.html
		/*public static void ShowBeepMessage(
			System.String message,
			System.Byte[] fileData,
			System.Int32? repeatCount = null)
		{
			// play
			//EFS.RC.CommunicatorContext.Common.PlaySound playSound = new EFS.RC.CommunicatorContext.Common.PlaySound(fileData);
			//playSound.BeginPlay();

			// show message
			ShowSuccessMessage(message);
		}*/

		/// GetCurrentPresenceName
		public static System.String GetCurrentPresenceName(Microsoft.Lync.Model.Conversation.Conversation lyncConversation)
		{
			Microsoft.Lync.Model.LyncClient lyncClient = Microsoft.Lync.Model.LyncClient.GetClient();
			System.Object oDisplayName = lyncClient.Self.Contact.GetContactInformation(Microsoft.Lync.Model.ContactInformationType.DisplayName);

			// ok
			return null != oDisplayName ? oDisplayName.ToString() : "Supervisor";
		}

		/// <summary>
		/// Get description
		/// </summary>
		/// <param name="seconds"></param>
		/// <returns></returns>
		public static System.String TimeSpanDescription(System.Int64 seconds)
		{
			// create time span
			System.TimeSpan tsLength = new System.TimeSpan(seconds);

			// create length text
			System.Text.StringBuilder sbLength = new System.Text.StringBuilder();

			// DAYS
			if (tsLength.Days > 0)
			{
				sbLength.AppendFormat("{0} {1}",
					tsLength.Days,
					tsLength.Days != 1 ? "days" : "day");
			}

			// HOURS
			if (tsLength.Hours > 0)
			{
				if (sbLength.Length > 0)
				{
					sbLength.Append(", ");
				}
				sbLength.AppendFormat("{0} {1}",
					tsLength.Hours,
					tsLength.Hours != 1 ? "hours" : "hour");
			}

			// MINUTES
			if (tsLength.Minutes > 0)
			{
				if (sbLength.Length > 0)
				{
					sbLength.Append(", ");
				}
				sbLength.AppendFormat("{0} {1}",
					tsLength.Minutes,
					tsLength.Minutes != 1 ? "minutes" : "minute");
			}

			// SECONDS
			if (0 == sbLength.Length || tsLength.Seconds > 0)
			{
				if (sbLength.Length > 0)
				{
					sbLength.Append(", ");
				}
				sbLength.AppendFormat("{0} {1}",
					tsLength.Seconds,
					tsLength.Seconds != 1 ? "seconds" : "second");
			}

			// conversation length
			return sbLength.ToString();
		}
		#endregion
	}

}